package com.xxx.tomcat.jdbc;

import org.apache.tomcat.jdbc.pool.ConnectionPool;
import org.apache.tomcat.jdbc.pool.JdbcInterceptor;
import org.apache.tomcat.jdbc.pool.PooledConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;

/**
 * Description
 * ...
 * DATE 2017/12/12.
 *
 * @author Mr. Chun.
 */
public class DemoInterceptor extends JdbcInterceptor {

    private final static Logger logger = LoggerFactory.getLogger(DemoInterceptor.class);

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String name = method.getName();
        if (CLOSE_VAL.equals(name)) {
            logger.debug(String.format("Returning Connection to Pool [%s]", proxy));
        }
        return super.invoke(proxy, method, args);
    }

    @Override
    public void reset(ConnectionPool parent, PooledConnection con) {
        if (parent != null && con != null) {
            logger.debug(
                    String.format("Getting Connection [%s], Pool active=[%s], idle=[%s]",
                            con.toString(),
                            parent.getActive(),
                            parent.getIdle()));
        }
    }

    @Override
    public void disconnected(ConnectionPool parent, PooledConnection con, boolean finalizing) {
        if (parent != null && con != null) {
            logger.debug(
                    String.format("Closing Connection [%s], Pool active=[%s], idle=[%s]",
                            con.toString(),
                            parent.getActive(),
                            parent.getIdle()));
        }
    }
}
